package com.mengxuegu.oauth2.server.mobile;

import com.mengxuegu.oauth2.web.entities.SysPermission;
import com.mengxuegu.oauth2.web.entities.SysRole;
import com.mengxuegu.oauth2.web.entities.SysUser;
import com.mengxuegu.oauth2.web.mapper.SysRoleMapper;
import com.mengxuegu.oauth2.web.service.SysPermissionService;
import com.mengxuegu.oauth2.web.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.assertj.core.util.Lists;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 自定义的获取用户信息
 */
@Service
@RequiredArgsConstructor
public class CustomPhoneUserDetailService {
    private final SysUserService sysUserService;
    private final SysPermissionService sysPermissionService;
    private final SysRoleMapper sysRoleMapper;

    public UserDetails loadByPhone(String phone, String code) {
        if (!"123456".equals(code)) {
            throw new InvalidGrantException("验证码错误或已过期");
        }
        SysUser sysUser = findSysUser(phone);
        List<String> permissionCodes = findSysPermission(sysUser);
        List<String> roleCodes = findSysRole(sysUser);
        roleCodes.addAll(permissionCodes);
        setAuthorities(sysUser, roleCodes);
        return sysUser;
    }

    public SysUser findSysUser(String usernameOrMobile) {
        return sysUserService.findByMobile(usernameOrMobile);
    }

    public List<String> findSysPermission(SysUser sysUser) throws UsernameNotFoundException {
        if (sysUser == null) {
            throw new UsernameNotFoundException("未查询到有效用户信息");
        }
        List<SysPermission> sysPermissions = sysPermissionService.findByUserId(sysUser.getId());
        sysUser.setPermissions(sysPermissions);

        List<String> codeList = Lists.newArrayList();
        sysPermissions.forEach(permission -> codeList.add(permission.getCode()));
        return codeList;
    }

    public List<String> findSysRole(SysUser sysUser) {
        if (sysUser == null) {
            throw new UsernameNotFoundException("未查询到有效用户信息");
        }
        List<SysRole> sysRoles = sysRoleMapper.findByUserId(sysUser.getId());
        sysUser.setRoleList(sysRoles);
        List<String> roleCodeList = Lists.newArrayList();
        sysRoles.forEach(role -> roleCodeList.add(role.getRoleCode()));
        return roleCodeList;
    }

    public void setAuthorities(SysUser sysUser, List<String> roleCodeList) {
        List<GrantedAuthority> authorities = Lists.newArrayList();
        roleCodeList.forEach(roleCode -> authorities.add(new SimpleGrantedAuthority(roleCode)));
        sysUser.setAuthorities(authorities);
    }

}
